<?php 
/**
 * 商城区域划分文件
*/

define('IN_ECS', true);

require(dirname(__FILE__) . '/includes/init.php');
require_once(ROOT_PATH . '/includes/lib_order.php');
/*------------------------------------------------------ */
//-- 框架
/*------------------------------------------------------ */
if ($_REQUEST['act'] == '')
{
    exit;
}

//区域列表
if ($_REQUEST['act']=='list') {
    $smarty->assign('ur_here',  '<a href="area.php?act=list">区域列表</a> ');//当前位置
    $smarty->assign('action_link', array('href'=>'area.php?act=add',
        'text' => '添加区域'));//右边按钮
    $smarty->assign('full_page', 1);
    // 查询全部区域
    $area = $db->getALL("SELECT * FROM " .$ecs->table('service_area'));
    // 重组数据内容 获取所辖地区
    foreach ($area as $key => $value) {
    		$area[$key]['region']=area($value['region_array']);
    }
    $smarty->assign('area',$area);
    $smarty->display('service_list.htm');
}
//编制区域
elseif ($_REQUEST['act']=='edit') {
	$area_id=empty($_REQUEST['id'])?'':$_REQUEST['id'];
	$area = $db->getROW("SELECT * FROM " .$ecs->table('service_area')." WHERE area_id='$area_id' ");
	$area_info=area($area['region_array']);
	$smarty->assign('regions',         $area_info['region_id']);
	$smarty->assign('area_name',         $area['area_name']);
    $smarty->assign('ur_here',         '编辑区域');
    $smarty->assign('countries',        get_regions());
    $smarty->assign('area_id',        $area_id);
    $smarty->assign('default_country',  1);
    $smarty->display('service_edit.htm');
}
//添加区域
elseif ($_REQUEST['act']=='add') {
    $smarty->assign('ur_here',         '添加区域');
    $smarty->assign('countries',        get_regions());
    $smarty->assign('default_country',  1);
    $smarty->display('service_add.htm');
}
//处理更新数据
elseif ($_REQUEST['act']=='updata') {
	//接受表单数据 区域名称和地区ID
	$regions=$_POST['regions'];
	$area_name=$_POST['area_name'];
    $area_id=$_POST['area_id'];
	foreach ($regions as $key => $value) {
            $region_id = $db->getOne("SELECT region_type FROM " .$ecs->table('region')." WHERE region_id='$value' ");
            switch ($region_id) {
                case '1':
                    $regions[$key]="{p".$value."}";
                    break;
                case '2':
                    $regions[$key]="{c".$value."}";
                    break;
                case '3':
                    $regions[$key]="{d".$value."}";
                    break;                    
                default:
                    # code...
                    break;
            }
    }
    $region= implode(',',$regions);
    $sql="UPDATE ".$ecs->table('service_area')." SET region_array='$region',area_name='$area_name' WHERE area_id='$area_id'";
    $return=$db->query($sql);
    if($return){
        header("Location: area.php?act=list");

    }
}
//处理插入数据
elseif ($_REQUEST['act']=='into_data') {
        //接受表单数据 区域名称和地区ID
    $regions=$_POST['regions'];
    $area_name=$_POST['area_name'];
    foreach ($regions as $key => $value) {
            $region_id = $db->getOne("SELECT region_type FROM " .$ecs->table('region')." WHERE region_id='$value' ");
            switch ($region_id) {
                case '1':
                    $regions[$key]="{p".$value."}";
                    break;
                case '2':
                    $regions[$key]="{c".$value."}";
                    break;
                case '3':
                    $regions[$key]="{d".$value."}";
                    break;                    
                default:
                    # code...
                    break;
            }
    }

    $region= implode(',',$regions);
    $sql = "INSERT INTO ".$ecs->table('service_area')." (region_array, area_name) VALUES ('$region', '$area_name')";
    $db->query($sql);
    $id = $db->insert_id();
    $sql="INSERT INTO ".$ecs->table('goods_stock')." (goods_id, goods_attr_id, goods_sku, area_id, goods_number) SELECT goods_id, goods_attr_id, goods_sku, '$id', '0' FROM ".$ecs->table('goods_stock')." WHERE area_id = '1'";
    $return=$db->query($sql);
    if($return){
        header("Location: area.php?act=list");
    }
}
//删除区域
elseif ($_REQUEST['act']=='remove_area') {
    $id = intval($_GET['id']);
    $db->query('DELETE FROM '.$ecs->table('service_area').' WHERE area_id='.$id);
    $db->query('DELETE FROM '.$ecs->table('goods_stock').' WHERE area_id='.$id);
    // 查询全部区域
    $area = $db->getALL("SELECT * FROM " .$ecs->table('service_area'));
    // 重组数据内容 获取所辖地区
    foreach ($area as $key => $value) {
            $area[$key]['region']=area($value['region_array']);
    }
    $smarty->assign('area',$area);
    make_json_result($smarty->fetch('service_list.htm'));
}

//导蛋送区域列表
if ($_REQUEST['act']=='daodansong_list') {
    $smarty->assign('ur_here',  '<a href="area.php?act=daodansong_list">区域列表</a> ');//当前位置
    $smarty->assign('action_link', array('href'=>'area.php?act=daodansong_add',
        'text' => '添加区域'));//右边按钮
    $smarty->assign('full_page', 1);
    // 查询全部区域
    $area = $db->getALL("SELECT * FROM " .$ecs->table('daodansong_area'));
    // 重组数据内容 获取所辖地区
    foreach ($area as $key => $value) {
            $area[$key]['region']=area($value['region_array']);
    }
    $smarty->assign('area',$area);
    $smarty->display('daodansong_list.htm');
}
//导蛋送编制区域
elseif ($_REQUEST['act']=='daodansong_edit') {
    $area_id=empty($_REQUEST['id'])?'':$_REQUEST['id'];
    $area = $db->getROW("SELECT * FROM " .$ecs->table('daodansong_area')." WHERE area_id='$area_id' ");
    $area_info=area($area['region_array']);
    $smarty->assign('regions',         $area_info['region_id']);
    $smarty->assign('area_name',         $area['area_name']);
    $smarty->assign('ur_here',         '编辑区域');
    $smarty->assign('countries',        get_regions());
    $smarty->assign('area_id',        $area_id);
    $smarty->assign('default_country',  1);
    $smarty->display('daodansong_edit.htm');
}
//导蛋送添加区域
elseif ($_REQUEST['act']=='daodansong_add') {
    $smarty->assign('ur_here',         '添加区域');
    $smarty->assign('countries',        get_regions());
    $smarty->assign('default_country',  1);
    $smarty->display('daodansong_add.htm');
}
//导蛋送处理更新数据
elseif ($_REQUEST['act']=='daodansong_updata') {
    //接受表单数据 区域名称和地区ID
    $regions=$_POST['regions'];
    $area_name=$_POST['area_name'];
    $area_id=$_POST['area_id'];
    $region= implode(',',$regions);
    $sql="UPDATE ".$ecs->table('daodansong_area')." SET region_array='$region',area_name='$area_name' WHERE area_id='$area_id'";
    $return=$db->query($sql);
    if($return){
        header("Location: area.php?act=daodansong_list");
    }
}
//导蛋送处理插入数据
elseif ($_REQUEST['act']=='daodansong_into_data') {
    //接受表单数据 区域名称和地区ID
    $regions=$_POST['regions'];
    $area_name=$_POST['area_name'];
    $region= implode(',',$regions);
    $sql = "INSERT INTO ".$ecs->table('daodansong_area')." (region_array, area_name) VALUES ('$region', '$area_name')";
    $db->query($sql);
    header("Location: area.php?act=daodansong_list");
}
//导蛋送删除区域
elseif ($_REQUEST['act']=='daodansong_remove_area') {
    $id = intval($_GET['id']);
    $db->query('DELETE FROM '.$ecs->table('daodansong_area').' WHERE area_id='.$id);
    // 查询全部区域
    $area = $db->getALL("SELECT * FROM " .$ecs->table('daodansong_area'));
    // 重组数据内容 获取所辖地区
    foreach ($area as $key => $value) {
        $area[$key]['region']=area($value['region_array']);
    }
    $smarty->assign('area',$area);
    make_json_result($smarty->fetch('daodansong_list.htm'));
}


























 ?>